﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Entity;
using System.Text;
using System.Threading.Tasks;
using Model;
using IDAL;
namespace DAL
{
    class SqlordersDAL : IordersDAL
    {
        GameGOEntities db = DbContextFactory.CreateDbContext();
        public void AddOrders(orders o)
        {
            db.orders.Add(o);
            db.SaveChanges();
        }
        public orders GetLastOrder(int uid)
        {
            var order = db.orders.Where(o => o.userID == uid).OrderByDescending(a => a.ID).FirstOrDefault();
            return order;
        }
        public orders GetOrderByID(int id)
        {
            var order = db.orders.Where(o => o.ID == id).FirstOrDefault();
            return order;
        }
        public void UpdateOrder(orders o)
        {
            db.Set<orders>().Attach(o);
            db.Entry<orders>(o).State = EntityState.Modified;
            db.SaveChanges();
        }
        public IQueryable<orders> GetOrdersByUid(int uid)
        {
            var order = db.orders.Where(o => o.userID == uid).OrderByDescending(a => a.ID);
            return order;
        }
        public void Confirm(int oid)
        {
            var o = db.orders.Where(a => a.ID == oid).FirstOrDefault();
            o.nowState = 3;
            db.Set<orders>().Attach(o);
            db.Entry<orders>(o).State = EntityState.Modified;
            db.SaveChanges();
        }
        public IEnumerable<orders> GetSendOrder()
        {
            return db.orders.Where(o => o.nowState == 1);
        }
        public void ModifyOrderState(int o, int state)
        {
            var t = db.orders.Where(p => p.ID == o).FirstOrDefault();
            t.nowState = state;
            db.SaveChanges();
        }
    }
}
